/*
 * server_connectionless.c
 *
 *  Created on: 2012-5-2
 *      Author: jawinton
 */

#include "../apue.h"
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>
#include <syslog.h>

#define BUFLEN 128
#define MAXADDRLEN 256

#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 256
#endif

extern int initserver (const struct sockaddr* addr, socklen_t alen, int type, int protocol, int qlen);

void serve (int sockfd) {
	int n;
	socklen_t alen;
	FILE *fp;
	char buf[BUFLEN];
	char abuf[MAXADDRLEN];

	while(1) {
		alen = MAXADDRLEN;
		if ((n = recvfrom(sockfd, buf, BUFLEN, 0, (struct sockaddr *)abuf, &alen)) < 0) {
			syslog(LOG_ERR, "ruptimed: recvfrom error: %s", strerror(errno));
			exit(1);
		}
		if ((fp = popen("/usr/bin/uptime", "r")) == NULL) {
			sprintf(buf, "error: %s\n",strerror(errno));
			sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)abuf, alen);
		} else {
			if (fgets(buf, BUFLEN, fp) != NULL)
				sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *)abuf, alen);
			pclose(fp);
		}
	}
}

int main (int argc, char *argv[]) {
	struct addrinfo *ailist, *aip;
	struct addrinfo hint;
	int sockfd, err, n;
	char *host;
	pid_t pid;

	if (argc != 1) err_quit("usage: ruptimed");
#ifdef _SC_HOST_NAME_MAX
	n = sysconf(_SC_HOST_NAME_MAX);
	if (n < 0)
#endif
		n = HOST_NAME_MAX;
	host = malloc(n);
	if (host == NULL) err_sys("malloc error");
	if (gethostname(host, n) < 0) err_sys("gethostname error");
	hint.ai_addr = NULL;
	hint.ai_addrlen = 0;
	hint.ai_canonname = NULL;
	hint.ai_family = 0;
	hint.ai_flags = AI_CANONNAME;
	hint.ai_next = NULL;
	hint.ai_protocol = 0;
	hint.ai_socktype = SOCK_DGRAM;
	if ((err = getaddrinfo(host, "ruptime", &hint, &ailist)) != 0) {
		syslog(LOG_ERR, "ruptimed: getaddrinfo error: %s", gai_strerror(err));
		exit(1);
	}
#if 0
	for (aip = ailist; aip != NULL; aip = aip->ai_next) {
		syslog(LOG_DAEMON,"%s: %d\n", aip->ai_canonname, pid);
	}
#endif
	for (aip = ailist; aip != NULL; aip = aip->ai_next) {
		print_family(aip);
		print_type(aip);
		print_protocol(aip);
		if ((sockfd = initserver(aip->ai_addr, aip->ai_addrlen, aip->ai_socktype, aip->ai_protocol, 0)) >= 0) {
			serve(sockfd);
			exit(0);
		} else {
			syslog(LOG_DAEMON, "initserver error: sockfd = %d", sockfd);
		}
	}

	exit(1);
}


